package com.justgame.project.base.listener;

import com.justgame.project.cloud.common.util.HttpContextUtil;
import com.justgame.project.cloud.common.util.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Component
@Slf4j
public class RequestListener implements ServletRequestListener {
    public RequestListener() {

    }

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
        HttpSession session = request.getSession();
        session.setAttribute("userId", ShiroUtils.getUserId(request));
        session.setAttribute("ip", HttpContextUtil.getIp(request));
        log.info("{}:::正在访问", session.getAttribute("ip"));
    }
    
    @Override
    public void requestDestroyed(ServletRequestEvent sre) {

    }
}
